//
//  labelbtnViewController.m
//  UI_HLJ_Frame
//
//  Created by  毕宏伟 on 15/7/4.
//  Copyright (c) 2015年 毕宏伟. All rights reserved.
//

#import "labelbtnViewController.h"
#import "UIImageView+WebCache.h"
#import "AFNetworking.h"
#import "labelModel.h"
#import "mylabelDetailCollectionViewCell.h"
#import "shuixiaDetailViewController.h"
#import "shuixiaworksModel.h"
@interface labelbtnViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,retain)UICollectionView *collectionView;

@property(nonatomic,retain)NSMutableArray *labelArr;

@property(nonatomic,retain)NSMutableArray *labelPicArr;

@property(nonatomic,retain)NSMutableArray *labeltextArr;

@end

@implementation labelbtnViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createData];
    
    [self createView];

    
    
    
    
    
}

- (void)createView
{
   
    //创建collectionview
    UICollectionViewFlowLayout *flow =[[UICollectionViewFlowLayout alloc]init];
    flow.itemSize =CGSizeMake(100, 100);
    flow.minimumInteritemSpacing =0;
    flow.minimumLineSpacing =20;
    
    //
    self.collectionView =[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-49) collectionViewLayout:flow];
    self.collectionView.backgroundColor =[UIColor colorWithRed:1.000 green:0.404 blue:0.651 alpha:1.0];
    self.collectionView.alpha =0.8;
    self.collectionView.delegate =self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    
    //注册cell
    
    [self.collectionView registerClass:[mylabelDetailCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    
    
}




- (void)createData
{
    self.labelArr = [NSMutableArray array];
    self.labelPicArr = [NSMutableArray array];
    self.labeltextArr = [NSMutableArray array];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://hunliji.com/v1/api/app/marks/recommend" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = responseObject;
        NSArray *Arr = dic[@"marks"];
        for (NSDictionary *marksdic in Arr) {
            
            labelModel *label = [[labelModel alloc]init];
            [label setValuesForKeysWithDictionary:marksdic];
            
            [self.labelArr addObject:label];
            [self.labelPicArr addObject:label.image_path];
            
            [self.labeltextArr addObject:label.name];
            
            [label release];
            
        }
        [self.collectionView reloadData];
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];

}


//实现collectionview的协议方法.

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.labelArr.count;
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    mylabelDetailCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    labelModel *la =self.labelArr[indexPath.row];
    [cell.labelImage sd_setImageWithURL:[NSURL URLWithString:la.image_path]];

    return cell;
    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    shuixiaDetailViewController *shuiVC = [[shuixiaDetailViewController alloc]init];
    labelModel *la =self.labelArr[indexPath.row];
    shuiVC.idurl =la.bid;
    
    [self.navigationController pushViewController:shuiVC animated:YES];
    [shuiVC release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
